package com.knight.activemq.subscribers;


import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created by YQ on 2017/6/15.
 * 消息发布者
 */
public class Publishers {
    public static void main(String[] args) throws Exception {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://0.0.0.0:61616");

        Connection connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
        Destination helloTopic = session.createTopic("helloTopic");
        MessageProducer producer = session.createProducer(helloTopic);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

        for (int i = 0; i < 6; i++) {
            TextMessage message = session.createTextMessage();
            message.setText("Hello World Publish " + i);
            producer.send(message);
        }

        if (connection != null) {
            connection.close();
        }
    }
}
